$(document).on("click", "#saveBtn", function (ev) {
	var obj = '';
	var orgTitle = $('#orgTitle').html();
	$('.droppable .form-group').each(function(){
		var str = '{';
		var i = 1;
		$(this).find('input').each(function(){
			var id = this.id;
			if(id == 'textTitle' || id == 'radioTitle' || id == 'checkboxTitle' 
				|| id == 'scoreTitle' || id == 'sortTitle'){
				str += '"id":"' + id + '",';
				str += '"title":"' + this.value + '",';
			} else if (id == 'scoreQuestion' || id == 'sortQuestion'){
				var tip = "answer_" + i++;
				str += '"' + tip + '":"' + this.value + '",';
			} else if (id != 'textQuestion') {
				var tip = "answer_" + i++;
				str += '"' + tip + '":"' + this.nextElementSibling.innerHTML + '",';
			}
		});
		str = str.substr(0, str.length - 1);
		str += '}';
		obj += str + ";";
	});
	obj = obj.substr(0, obj.length - 1);
	if(obj.length < 1){
		alert('没有可保存的题目');
		return false;
	}
	$.ajax({
		type : "post",
		url : "answer/showAnswer",
		data: {
			orgTitle: orgTitle,
			obj: obj
		},
		dataType : 'json',
		async:false,
		success : function(data) {
			var url = 'survey/querySurvey?id=' + data.success;
			var host = window.location.host;
			var pathName = window.document.location.pathname;
			window.location.href = 'url.jsp?url=' + host + pathName + url;
		},
		error:function(){
			alert('failed');
		}
	});
});

$(document).on("click", "#previewBtn", function(){
	var obj = '';
	var orgTitle = $('#orgTitle').html();
	$('.droppable .form-group').each(function(){
		var str = '{';
		var i = 1;
		$(this).find('input').each(function(){
			var id = this.id;
			if(id == 'textTitle' || id == 'radioTitle' || id == 'checkboxTitle' 
				|| id == 'scoreTitle' || id == 'sortTitle'){
				str += '"id":"' + id + '",';
				str += '"title":"' + this.value + '",';
			} else if (id == 'scoreQuestion' || id == 'sortQuestion'){
				var tip = "answer_" + i++;
				str += '"' + tip + '":"' + this.value + '",';
			} else if (id != 'textQuestion') {
				var tip = "answer_" + i++;
				str += '"' + tip + '":"' + this.nextElementSibling.innerHTML + '",';
			}
		});
		str = str.substr(0, str.length - 1);
		str += '}';
		obj += str + ";";
	});
	obj = obj.substr(0, obj.length - 1);
	if(obj.length < 1){
		alert('没有可预览的题目');
		return false;
	}
	$.ajax({
		type : "post",
		url : "answer/showAnswer",
		data: {
			orgTitle: orgTitle,
			obj: obj,
			type: 'preview'
		},
		dataType : 'json',
		async:false,
		success : function(data) {
			window.open('survey/querySurvey?id=' + data.success + '&type=preview');
		},
		error:function(){
			alert('failed');
		}
	});
});